package com.polaris.live.popup

import android.content.Context
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.common.R.color
import com.polaris.live.common.ext.formatNumberWithK
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.DialogWeeklyStarBinding
import com.polaris.live.net.gameService
import com.polaris.live.resp.back_resp.ActSelfRanksVo
import com.polaris.live.translation.R.string.duration_min
import com.polaris.live.translation.R.string.people_connected
import com.polaris.live.translation.R.string.week_gift
import com.polaris.live.translation.R.string.week_rank
import com.polaris.live.translation.R.string.week_rank_delta
import com.polaris.live.utils.XPopupUtils

/**
 * 周星详情弹窗
 * @author Created by 天晴 on 2024/5/7 16:43
 * @since 1.0.0
 **/
class WeeklyStarPopup(
    context: Context,
    private val viewModel: BaseViewModel,
    private var mData: ActSelfRanksVo,
    private val mLastIndex: Int = 3,
    private val mIndexCallBack: ((index: Int) -> Unit)? = null
) : CenterPopupView(context) {

    private var lastUpdataTime = 0L
    override fun getImplLayoutId(): Int {
        return R.layout.dialog_weekly_star
    }

    private lateinit var mBinding: DialogWeeklyStarBinding

    private var index = 3
    override fun onCreate() {
        super.onCreate()
        lastUpdataTime = System.currentTimeMillis()

        index = mLastIndex

        mBinding = DialogWeeklyStarBinding.bind(popupImplView)

        mBinding.apply {
            closeImg.setOnSingleClickListener {
                dismiss()
            }
            setData(index)
            connectionTv.setOnSingleClickListener {
                index = 1
                mIndexCallBack?.invoke(index)
                setData(index)
                updateDate()
            }
            durationTv.setOnSingleClickListener {
                index = 2
                mIndexCallBack?.invoke(index)
                setData(index)
                updateDate()
            }
            giftTv.setOnSingleClickListener {
                index = 3
                mIndexCallBack?.invoke(index)
                setData(index)
                updateDate()
            }

        }
    }

    fun setData(index: Int) {
        mBinding.apply {
            when (index) {
                1 -> {
                    connectionTv.setTextColor(ColorUtils.getColor(R.color.white))
                    connectionTv.setBackgroundResource(R.drawable.weekstar_bg_index)
                    durationTv.setTextColor(ColorUtils.getColor(color.color_202028))
                    durationTv.setBackgroundResource(R.drawable.r20_shape_ee59f6_stork1)
                    giftTv.setTextColor(ColorUtils.getColor(color.color_202028))
                    giftTv.setBackgroundResource(R.drawable.r20_shape_ee59f6_stork1)
                    mData.connection.also {
                        tv1.text =
                            StringUtils.getString(people_connected, it?.score.formatNumberWithK())
                        tv2.text = StringUtils.getString(week_rank, it?.rank.formatNumberWithK())
                        tv3.text = StringUtils.getString(
                            week_rank_delta,
                            it?.rankDelta.formatNumberWithK()
                        )
                        if (it?.rank == 1L) {
                            tv3.visibility = GONE
                        } else {
                            tv3.visibility = VISIBLE
                        }
                    }
                }

                2 -> {
                    connectionTv.setTextColor(ColorUtils.getColor(color.color_202028))
                    connectionTv.setBackgroundResource(R.drawable.r20_shape_ee59f6_stork1)
                    durationTv.setTextColor(ColorUtils.getColor(color.white))
                    durationTv.setBackgroundResource(R.drawable.weekstar_bg_index)
                    giftTv.setTextColor(ColorUtils.getColor(color.color_202028))
                    giftTv.setBackgroundResource(R.drawable.r20_shape_ee59f6_stork1)
                    mData.duration.also {
                        tv1.text =
                            StringUtils.getString(duration_min, it?.score.formatNumberWithK())
                        tv2.text = StringUtils.getString(week_rank, it?.rank.formatNumberWithK())
                        tv3.text =
                            StringUtils.getString(
                                week_rank_delta,
                                it?.rankDelta.formatNumberWithK()
                            )
                        if (it?.rank == 1L) {
                            tv3.visibility = GONE
                        } else {
                            tv3.visibility = VISIBLE
                        }
                    }
                }

                3 -> {
                    connectionTv.setTextColor(ColorUtils.getColor(color.color_202028))
                    connectionTv.setBackgroundResource(R.drawable.r20_shape_ee59f6_stork1)
                    durationTv.setTextColor(ColorUtils.getColor(color.color_202028))
                    durationTv.setBackgroundResource(R.drawable.r20_shape_ee59f6_stork1)
                    giftTv.setTextColor(ColorUtils.getColor(color.white))
                    giftTv.setBackgroundResource(R.drawable.weekstar_bg_index)
                    mData.gift.also {
                        tv1.text =
                            StringUtils.getString(week_gift, it?.score.formatNumberWithK())
                        tv2.text = StringUtils.getString(week_rank, it?.rank.formatNumberWithK())
                        tv3.text =
                            StringUtils.getString(
                                week_rank_delta,
                                it?.rankDelta.formatNumberWithK()
                            )
                        if (it?.rank == 1L) {
                            tv3.visibility = GONE
                        } else {
                            tv3.visibility = VISIBLE
                        }
                    }
                }
            }
        }

    }

    override fun getMaxWidth(): Int {
        return ScreenUtils.getScreenWidth()
    }

    override fun getPopupWidth(): Int {
        return ScreenUtils.getScreenWidth()
    }

    /**
     * 更新数据
     */
    private fun updateDate() {
        //点击时大于5s才请求新数据
        if (System.currentTimeMillis() - lastUpdataTime > 5000) {
            viewModel.requestLoading({ gameService.getWeeklyStarRanks() }, {
                lastUpdataTime = System.currentTimeMillis()
                mData = it
                setData(index)
            }, {
                ToastGravity.showGravity(it.errorMsg)
            })
        }
    }

    companion object {

        fun showPop(
            context: Context,
            owner: LifecycleOwner,
            viewModel: BaseViewModel,
            index: Int = 3,
            indexCallBack: ((index: Int) -> Unit)? = null
        ) {
            viewModel.requestLoading({ gameService.getWeeklyStarRanks() }, {
                XPopupUtils.showPopup(
                    WeeklyStarPopup(context, viewModel, it, index, indexCallBack),
                    {
                        hasShadowBg(true)
                    },
                    lifecycle = owner.lifecycle
                )
            }, {
                ToastGravity.showGravity(it.errorMsg)
            })
        }
    }
}